package it.koutifaris.travianmap.dal.sqlservices.travianserver;

import static it.koutifaris.testforclassutils.IsTestForAssert.assertIsTestFor;
import it.koutifaris.travianmap.dal.TravianServerService.TravianServer;
import it.koutifaris.travianmap.dal.sqlservices.AbstractServerServiceTest;

import java.util.Date;
import java.util.List;

import junit.framework.Assert;

public class SqlTravianServerServiceTest extends AbstractServerServiceTest {

	private SqlTravianServerService service;

	@Override
	protected void overridableSetUp() {
		service = new SqlTravianServerService(getDbHelper().getWritableDatabase());
	}

	public void testPkgAndName() {
		assertIsTestFor(SqlTravianServerService.class);
	}

	public void testGetServerFromEmptyTable() {
		TravianServer server = service.getServer(TEST_SERVER);
		Assert.assertNull(server);
	}

	public void testNewServer() {
		TravianServer testServer = service.newServer(TEST_SERVER);
		TravianServer server = service.getServer(testServer.getServerId());

		Assert.assertNotNull(server);
	}

	public void testGetServer() {
		TravianServer testServer = service.newServer(TEST_SERVER);
		TravianServer server = service.getServer(testServer.getServerId());

		Assert.assertEquals(TEST_SERVER, server.getServerId());
	}

	public void testListServers() {
		TravianServer testServerA = service.newServer("ts1.travian.com");
		TravianServer testServerB = service.newServer("ts2.travian.com");
		List<TravianServer> servers = service.listServers();

		Assert.assertEquals(2, servers.size());
		Assert.assertTrue(servers.contains(testServerA));
		Assert.assertTrue(servers.contains(testServerB));
	}

	public void testUpdateServer() {
		Date now = new Date();

		service.newServer(TEST_SERVER);
		service.updateServer(TEST_SERVER, now);
		TravianServer server = service.getServer(TEST_SERVER);

		Assert.assertEquals(now, server.getLastUpdate());
	}

	public void testUpdateServerRetValue() {
		Date now = new Date();

		service.newServer(TEST_SERVER);
		TravianServer server = service.updateServer(TEST_SERVER, now);

		Assert.assertEquals(now, server.getLastUpdate());
	}

}
